$(function () {
    var username = cookie.get('username'),
        userType = cookie.get('userType');
    var html = '';
    var countNumber = null;
    var token = null;
    var urlLink = null;
    var ediHost = Api.ediHost;
    var ediPort = Api.ediPort;
    $.ajax({
        url: '/api/v1/csraskboard/unreadcount?t=' + new Date().getTime(),
        success: function (res) {
            if (res.errorCode === 0) {
                // html += '<a href="javascript:;"><span>' + username + '</span><span class="u-down-arrow"></span></a>';
                html += '<a href="javascript:;" class="avatar-wrapper"><img class="avatar" src="images/avatar1.png" width="30px" height="30px" alt=""/><span class="u-down-arrow"></span></a>';
                html += '<div class="menu-content clearfix">';
                html += '<div class="menu-content-list">';
                html += '<ul class="m-lang-list m-profile-list">';
                html += '<li><a href="/basic_info">基本信息</a></li>';
                if(res.data.unReadCount > 0){
                  countNumber = res.data.unReadCount;
                  countNumber = countNumber>99?"···":countNumber;
                  html += '<li><a href="/notification">消息通知</a><span class="alert">' + countNumber + '</span></li>';
                }else{
                  html += '<li><a href="/notification">消息通知</a></li>';
                }

                if(userType == 1 || userType == 3){    //客户类型为1、3时。链接到物流的订单与账单
                    html += '<li><a href="/order_manage">我的订单</a></li>';
                    html += '<li><a href="/billManage">我的账单</a></li>';
                }else if(userType == 2 || userType == 256){    //客户类型为2、256时。链接到贸易的订单与账单
                    html += '<li><a href="/order_manage_trade">我的订单</a></li>';
                    html += '<li><a href="/billManage_trade">我的账单</a></li>';
                }

                html += '<li><a href="/user_security">安全中心</a></li>';
                html += '<li><a id="logout" href="javascript:;">退出登录</a></li>';
                html += '</ul>';
                html += '</div>';
                html += '</div>';

                $('#login').html(html);
                $('#logout').click(function () {
                    $.ajax({
                        url: "/api/v1/auth/logout",
                        success: function (res) {
                            cookie.delete('token');
                            cookie.delete('username');
                            cookie.delete("userId");
                            cookie.delete("primary");
                            cookie.delete("fullName");
                            cookie.delete("loginSuccess");
                            cookie.delete("sex");
                            cookie.delete("userType");
                            cookie.delete("tradingCurrency");
                            window.location.href = '/';
                        }
                    });
                });
            } else {    //在没有登录的情况下将cookie清理掉，解决node端拦截后退出cookie存在问题。
                // cookie.delete('token');
                cookie.delete('username');
                cookie.delete("userId");
                cookie.delete("primary");
                cookie.delete("fullName");
                cookie.delete("loginSuccess");
                cookie.delete("sex");
                cookie.delete("userType");
                cookie.delete("tradingCurrency");
                html = '<a href="/login" class="u-not-login">登录</a><a href="/register" class="u-not-register">注册</a>';
                $('#login').html(html);
            }
        }
    })

    $('#help-center').click(function () {
      // token = cookie.get('token');
      // if (token && typeof(token)!=="undefined" && token !== null) {
        urlLink = "http://"+ediHost+":"+ediPort;
        window.location.href = urlLink;
      // } else {
      //   window.location.href = "/login";
      // }
    });

});
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    };

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};
String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}

Date.prototype.getBeforeDate = function (n) {
    var n = n,s;
    var d = new Date();
    var year = d.getFullYear();
    var mon=d.getMonth()+1;
    var day=d.getDate();
    if(day <= n){
        if(mon>1) {
            mon=mon-1;
        }
        else {
            year = year-1;
            mon = 12;
        }
    }
    d.setDate(d.getDate()-n);
    year = d.getFullYear();
    mon=d.getMonth()+1;
    day=d.getDate();
    s = year+"-"+(mon<10?('0'+mon):mon)+"-"+(day<10?('0'+day):day);
    return s;
}
Date.prototype.getLastMonth = function () {
    var nowdays = new Date();
    var year = nowdays.getFullYear();
    var month = nowdays.getMonth();
    if(month==0)
    {
        month=12;
        year=year-1;
    }
    if (month < 10) {
        month = "0" + month;
    }
    var start = year + "-" + month + "-" + "01 00:00:00";//上个月的第一天
    var myDate = new Date(year, month, 0);
    var end = year + "-" + month + "-" + myDate.getDate()+" 23:59:59";//上个月的最后一天


    return {
        'start' : start ,
        'end' : end
    }

}
$.fn.scrollUnique = function() {
    return $(this).each(function() {
        var eventType = 'mousewheel';
        // 火狐是DOMMouseScroll事件
        if (document.mozHidden !== undefined) {
            eventType = 'DOMMouseScroll';
        }
        $(this).on(eventType, function(event) {
            // 一些数据
            var scrollTop = this.scrollTop,
                scrollHeight = this.scrollHeight,
                height = this.clientHeight;

            var delta = (event.originalEvent.wheelDelta) ? event.originalEvent.wheelDelta : -(event.originalEvent.detail || 0);

            if ((delta > 0 && scrollTop <= delta) || (delta < 0 && scrollHeight - height - scrollTop <= -1 * delta)) {
                // IE浏览器下滚动会跨越边界直接影响父级滚动，因此，临界时候手动边界滚动定位
                this.scrollTop = delta > 0? 0: scrollHeight;
                // 向上滚 || 向下滚
                event.preventDefault();
            }
        });
    });
};

/*错误定位代码start*/
function startScroll(currentErrorEle, parentHasScrollNode) {
    if(!parentHasScrollNode.offset()) {
        $(document).scrollTop(currentErrorEle.offset().top - 30);
    } else {
        parentHasScrollNode.scrollTop(currentErrorEle.offset().top - parentHasScrollNode.offset().top + parentHasScrollNode.scrollTop() - 10);
    }
}
function scrollToErrorView(parentHasScrollNode) {
    setTimeout(function() {
        var inputEles = $("[type=text]", parentHasScrollNode);
        var inputElesinputEles = $("textArea", parentHasScrollNode);
        var eles = $.merge(inputEles, inputElesinputEles);
        for(var index = 0; index < inputEles.length; index++) {
            if($(inputEles[index]).css("border-bottom-color") == "rgb(255, 0, 0)") {
                startScroll($(inputEles[index]), parentHasScrollNode);
                break;
            }
        }
    }, 30);
}
/*错误定位代码end*/
